﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Reflection;
using System.Text.RegularExpressions;
using System.IO;
using Little.Soldier.SWIFT;
using Little.Soldier.SWIFT.Validation;
using Little.Soldier.SWIFT.Messages.Category2;
using Little.Soldier.SWIFT.Messages.Category1;

namespace ConsoleApplication15
{
    class Program
    {
        //private static SWIFTFramework.Messages.Category1.MT192 msg192;
        //private static SWIFTFramework.Messages.Category1.MT199 msg199;
        //private static SWIFTFramework.Messages.Category1.MT103 msg103;
        //private static SWIFTFramework.Messages.Category1.MT103PLUS msg103PLUS;
        //private static SWIFTFramework.Messages.Category2.MT202 msg202;

        static void Main(string[] args)
        {
            string text = ReadInputFile(@"D:\SWIFT Framework.NET V2011.3\ConsoleApplication15\bin\Debug\2013-10-31T045608.txt");

            //For reading the message from source text use the GetMessage()  method. You can also read multiple 
            //messages from single file using the GetMessages() method. SWIFT Parser creates the new 
            //SwiftMessage object, filled with as much information as it is able to get from the source text. 
            //It provides a list of errors as well. 

//            string text = @"{1:F01ARTGIDJAAXXX2636277339}{2:O6711059140112DXDXXXXXDXXX00000151061401130632N}{4:
//:16R:GENL
//:20C::SEME//USD CORRESPOND
//:23G:NEWM
//:95P::SUBM//PIBAAFKA
//:22F::UDTP//CASH
//:16S:GENL
//:16R:SSIDET
//:22H::SSIP//NEWS
//:11A::SETT//USD
//:98A::EFFD//20140115
//:22F::MARK//ANYY
//:16R:CSHPRTY
//:95P::BENM//BOTKJPJT
//:97A::CASH//6530457167
//:16S:CSHPRTY
//:16S:SSIDET
//-}{5:{CHK:9054CF259CC3}}{S:{SPD:}{SAC:}{COP:P}}";

            Console.ReadLine();
            List<ValidationError> parseError = new List<ValidationError>();
            List<SwiftMessage> messages = SwiftParser.Instance.GetMessages(text, (char)'$', parseError);
            Console.WriteLine("Errors: {0}", parseError.Count);
            Console.WriteLine();

            //SwiftMessage messages = SwiftParser.Instance.GetMessage(text, parseError);

            //List<ValidationError> errors = new List<ValidationError>();

            //Tag13CValidator tag13CValidator = new Tag13CValidator("13C", true);
            //messages[0].Block4.Rules.Add(tag13CValidator);
            //messages[0].Block4.Validate(errors);


            //PropertyInfo propertyInfo = typeof(SwiftBlock2).GetProperty("IsInput");
            //SwiftBlockBase parentPropertyObject = messages[0].Block2;
            //bool allowEmptyValue = false;
            //IsInputMessageValidator isInputMessageValidator = new IsInputMessageValidator(propertyInfo, parentPropertyObject, allowEmptyValue);
            //messages[0].Block2.Rules.Add(isInputMessageValidator);
            //messages[0].Block2.Validate(errors);


            // Create ValidationRule object
            //ValidationRule_IsDecimal validationRule_IsDecimal = new ValidationRule_IsDecimal();

            // ValidationRule can be added into ValidatePropertyRule or ValidateTagRule
            //ValidatePropertyRule.Rules.Add(validationRule_IsDecimal);


            //Console.WriteLine("Errors: {0}", errors.Count);



            Console.WriteLine("XML:");
            Console.WriteLine(messages[0].ToXml());
            Console.WriteLine();

            //foreach (SwiftMessage message in messages)
            //{
            //    switch (message.MessageType)
            //    {
            //        case "192":
            //            msg192 = (SWIFTFramework.Messages.Category1.MT192)message;
            //            break;
            //        case "199":
            //            msg199 = (SWIFTFramework.Messages.Category1.MT199)message;
            //            break;
            //        case "103":
            //            SwiftBlock3 block3_4 = message.Block3;
            //            if (block3_4 != null)
            //            {
            //                SwiftTag tagByName = block3_4.GetTagByName("119");
            //                if (tagByName != null && tagByName.Value == "STP")
            //                    msg103PLUS = (SWIFTFramework.Messages.Category1.MT103PLUS)message;
            //                else
            //                    msg103 = (SWIFTFramework.Messages.Category1.MT103)message;
            //            }                        
            //            break;
            //        case "202":
            //            msg202 = (SWIFTFramework.Messages.Category2.MT202)message;
            //            break;
            //        default:
            //            break;
            //    }                
            //}

            MT103 msg = (MT103)messages[0];
            Console.WriteLine("LTAddress : {0}", msg.Block1.LTAddress);

            if (msg.Block2.IsInput)
            {
                SwiftBlock2Input input = (SwiftBlock2Input)msg.Block2;
                Console.WriteLine("DestinationAddress : {0}", input.DestinationAddress);
            }
            else
            {
                SwiftBlock2Output output = (SwiftBlock2Output)msg.Block2;
                Console.WriteLine("DestinationAddress : {0}", output.MessageInputReference.LTAddress);
            }
            Console.WriteLine("");
            //Console.WriteLine("Narrative_79");
            //Console.WriteLine(msg199.Narrative_79.Value);

            Console.WriteLine("Tag20");
            Console.WriteLine(msg.SendersReference_20.Value);
            Console.WriteLine("");
            Console.WriteLine("Tag32A");
            Console.WriteLine(msg.ValueDateCurrencyInterbankSettledAmount_32A.Date);
            Console.WriteLine(msg.ValueDateCurrencyInterbankSettledAmount_32A.Currency);
            Console.WriteLine(msg.ValueDateCurrencyInterbankSettledAmount_32A.Amount);
            Console.WriteLine("");
            Console.WriteLine("Tag50K");
            Console.WriteLine(msg.OrderingCustomer_50K.Value);
            Console.WriteLine("");
            Console.WriteLine("Tag52A");
            Console.WriteLine(msg.OrderingInstitution_52A.Value);
            Console.WriteLine("");
            Console.WriteLine("Tag57A");
            Console.WriteLine(msg.AccountWithInstitution_57A.Value);
            Console.WriteLine("");
            Console.WriteLine("Tag59");
            Console.WriteLine(msg.BeneficiaryCustomer_59.Value);

            Console.WriteLine("");
            Console.WriteLine("Success");
            Console.ReadLine();
        }

        public static string ReadInputFile(string fileWithfolderLocation)
        {
            try
            {
                FileInfo fileInfo = new FileInfo(fileWithfolderLocation);
                if (fileInfo.Exists)
                {
                    FileStream readFile = File.Open(fileInfo.FullName, FileMode.Open, FileAccess.Read);
                    StreamReader fileReader = new StreamReader(readFile);
                    string fileContents = fileReader.ReadToEnd();
                    fileReader.Close();
                    readFile.Close();

                    return fileContents;
                }
                else
                {
                    return null;
                }
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }

        public static SwiftMessage U0006U0003(SwiftMessage message)
        {
            switch (message.MessageType)
            {
                case "202":
                    SwiftBlock3 block3_3 = message.Block3;
                    if (block3_3 != null)
                    {
                        SwiftTag tagByName = block3_3.GetTagByName("119");
                        if (tagByName != null && tagByName.Value == "COV")
                            return (SwiftMessage)new MT202COV(message);
                    }
                    return (SwiftMessage)new MT202(message);
                case "199":
                    return (SwiftMessage)new MT199(message);
                case "192":
                    return (SwiftMessage)new MT192(message);
                case "103":
                    SwiftBlock3 block3_4 = message.Block3;
                    if (block3_4 != null)
                    {
                        SwiftTag tagByName = block3_4.GetTagByName("119");
                        if (tagByName != null && tagByName.Value == "STP")
                            return (SwiftMessage)new MT103PLUS(message);
                    }
                    return (SwiftMessage)new MT103(message);
                default:
                    return new SwiftMessage(message);
            }
        }
    }
}
